package com.sigmatrix.h5.web;

import com.alibaba.fastjson.JSONObject;
import com.sigmatrix.h5.utils.Constants;
import com.sigmatrix.sc.jdbc.common.lang.Page;
/**
 * 
 *@ClassName: Result
 *@Description: 返回结果
 *@author MH
 *@date 2017年9月22日 下午2:12:57
 */
public class Result<T> {
	
	private String status;//结果状态
	
	private String msg;//信息
	
	private Integer pageNo;//当前页数
	
	private Integer pageSize;
	
	private Integer total;//分页总记录数
	
	private T data;
	
	/**
	 * 
	 *@Title: ok
	 *@Description: 成功结果
	 *@return Result
	 *@author MH
	 *@date 2017年9月22日 下午2:11:43
	 *@param data
	 *@return
	 */
	public static <T> Result<T> ok(T data){
		return new Result<T>(Constants.DATA_SUCCESS, "操作成功", data); 
	}
	/**
	 * 
	 *@Title: error
	 *@Description: 失败结果
	 *@return Result<Void>
	 *@author MH
	 *@date 2017年11月6日 下午4:32:29
	 *@param status
	 *@param message
	 *@return
	 */
	public static <T> Result<T> error(String status,String message){
		return new Result<T>(status, message, null);
	}
	public static <T> Result<T> error(String message){
		return Result.error(Constants.DATA_FAILURE, message);
	}
	public static <T, K extends Exception> Result<T> error(K e){
		
		return Result.error(e.getMessage());
	}
	@SuppressWarnings("rawtypes")
	public Result(String status, String msg, T data) {
		super();
		this.status = status;
		this.msg = msg;
		this.data = data;
		if(data instanceof Page){
			Page p;
			this.pageNo = (p = (Page)data).getPageNo();
			this.pageSize = p.getPageSize();
			this.total = p.getTotal();
		}
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Integer getPageNo() {
		return pageNo;
	}

	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public Object getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}
	public Result() {
	}
	
	@Override
	public String toString() {
		return JSONObject.toJSONString(this);
	}
}
